/*
 * @(#)MainForm.java   13/08/20
 * 
 * Copyright (c) 2013 DieHard Development
 *
 * All rights reserved.
   Released under the BSD 3 clause license
Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice, this 
    list of conditions and the following disclaimer. Redistributions in binary 
    form must reproduce the above copyright notice, this list of conditions and 
    the following disclaimer in the documentation and/or other materials 
    provided with the distribution. Neither the name of the DieHard Development 
    nor the names of its contributors may be used to endorse or promote products 
    derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 *
 *
 *
 */


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package showtest;

//~--- non-JDK imports --------------------------------------------------------

import showtest.exceptions.PreexistingEntityException;

//~--- JDK imports ------------------------------------------------------------

import java.awt.event.ActionEvent;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import javax.swing.AbstractAction;
import javax.swing.Action;

/**
 *
 * @author TJ
 */
@SuppressWarnings("rawtypes")
public final class MainForm extends javax.swing.JFrame {
    private static TableCellListener tcl = null;
    private static TableCellListener tclFleet = null;
    private static final long serialVersionUID = 1L;

    /** Field description */
    public static final String TRUCKID = "Truckid";

    /** Field description */
    public static final String TRUCKOWNER = "Truckowner";

    /** Field description */
    public static final String TRUCKMAKE = "Truckmake";

    /** Field description */
    public static final String TRUCKCLASS = "Truckclass";

    /** Field description */
    public static final String VOTES = "Votes";

    /** Field description */
    public static final String ENTRY = "Entry";
    private static int _votes = 0;
    private static int _ID = 0;
    private static String _truckClass = "";
    private static String _truckMake = "";
    private static String _truckOwner = "";
    private static String _createEntryTruckClass = "";
    private static String _createEntryTruckMake = "";
    private static String _createEntryTruckOwner = "";
    private static int _fleetID = 0;
    private static String _fleetName = "";
    private static int _fleetVotes = 0;

    // End of variables declaration//GEN-END:variables
    private static final Logger LOG = Logger.getLogger(MainForm.class.getName());
    private Action actionFleet = new AbstractAction() {
        private static final long serialVersionUID = 1L;
        @Override
        public void actionPerformed(ActionEvent e) {
            tclFleet = (TableCellListener) e.getSource();
            getFleetTableValues();
            createFleetEdit();
            System.out.println("Row   : " + tclFleet.getRow());
            System.out.println("Column: " + tclFleet.getColumn());
            System.out.println("Old   : " + tclFleet.getOldValue());
            System.out.println("New   : " + tclFleet.getNewValue());
        }
    };

    /**
     *
     */
    public Action action = new AbstractAction() {
        private static final long serialVersionUID = 1L;
        @Override
        public void actionPerformed(ActionEvent e) {
            tcl = (TableCellListener) e.getSource();
            getEntryTableValues();
            createEntryEdit();
            System.out.println("Row   : " + tcl.getRow());
            System.out.println("Column: " + tcl.getColumn());
            System.out.println("Old   : " + tcl.getOldValue());
            System.out.println("New   : " + tcl.getNewValue());
        }
    };

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager ShowTestPUEntityManager;
    private java.util.List<showtest.Entry> entryList;
    @SuppressWarnings("unused")
    private java.util.List<showtest.Entry> entryList1;
    private javax.persistence.Query entryQuery;
    private javax.persistence.Query entryQuery1;
    private java.util.List<showtest.Fleet> fleetList;
    private javax.persistence.Query fleetQuery;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private com.xzq.osc.JocLabel jocLabel1;
    private com.xzq.osc.JocLabel jocLabel2;
    private com.xzq.osc.JocLabel jocLabel3;
    private com.xzq.osc.JocTable jocTable1;
    private com.xzq.osc.JocTable jocTable2;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;

    /**
     * Creates new form MainForm
     *
     * @throws PreexistingEntityException
     * @throws Exception
     */
    public MainForm() throws PreexistingEntityException, Exception {
        initComponents();
        tcl = new TableCellListener(jocTable1, getAction());
        tclFleet = new TableCellListener(jocTable2, getActionFleet());
    }

    /**
     * @return the action
     */
    public Action getAction() {
        return action;
    }

    /**
     * @param aAction the action to set
     */
    public void setAction(Action aAction) {
        action = aAction;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings({ "unchecked", "deprecation" })

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
        ShowTestPUEntityManager = java.beans.Beans.isDesignTime()
                                  ? null
                                  : javax.persistence.Persistence.createEntityManagerFactory(
                                      "ShowTestPU").createEntityManager();
        entryQuery = java.beans.Beans.isDesignTime()
                     ? null
                     : ShowTestPUEntityManager.createQuery("SELECT e FROM Entry e");
        entryList = java.beans.Beans.isDesignTime()
                    ? java.util.Collections.emptyList()
                    : entryQuery.getResultList();
        fleetQuery = java.beans.Beans.isDesignTime()
                     ? null
                     : ShowTestPUEntityManager.createQuery("SELECT f FROM Fleet f");
        fleetList = java.beans.Beans.isDesignTime()
                    ? java.util.Collections.emptyList()
                    : fleetQuery.getResultList();
        entryQuery1 = java.beans.Beans.isDesignTime()
                      ? null
                      : ShowTestPUEntityManager.createQuery("SELECT e FROM Entry e");
        entryList1 = java.beans.Beans.isDesignTime()
                     ? java.util.Collections.emptyList()
                     : entryQuery1.getResultList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jocTable1 = new com.xzq.osc.JocTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jocTable2 = new com.xzq.osc.JocTable();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jocLabel1 = new com.xzq.osc.JocLabel();
        jocLabel2 = new com.xzq.osc.JocLabel();
        jocLabel3 = new com.xzq.osc.JocLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ECLC Annual Truck Show");
        setAlwaysOnTop(true);
        setPreferredSize(new java.awt.Dimension(850, 420));
        setResizable(false);
        jocTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jocTable1.setColumnSelectionAllowed(true);
        jocTable1.setMaximumSize(new java.awt.Dimension(500, 0));
        jocTable1.setRowSelectionAllowed(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding =
            org.jdesktop.swingbinding.SwingBindings.createJTableBinding(
                org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, entryList, jocTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding =
            jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${truckid}"));

        columnBinding.setColumnName("Truck ID");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${truckowner}"));
        columnBinding.setColumnName("Truck Owner");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${truckmake}"));
        columnBinding.setColumnName("Truck Make");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${truckclass}"));
        columnBinding.setColumnName("Truck Class");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${votes}"));
        columnBinding.setColumnName("Votes");
        columnBinding.setColumnClass(Integer.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jocTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jocTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jocTable1);
        jocTable1.getColumnModel().getSelectionModel().setSelectionMode(
            javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jocTable1.getColumnModel().getColumn(0).setMinWidth(50);
        jocTable1.getColumnModel().getColumn(0).setPreferredWidth(50);
        jocTable1.getColumnModel().getColumn(0).setMaxWidth(60);
        jocTable1.getColumnModel().getColumn(1).setMinWidth(150);
        jocTable1.getColumnModel().getColumn(1).setPreferredWidth(150);
        jocTable1.getColumnModel().getColumn(1).setMaxWidth(170);
        jocTable1.getColumnModel().getColumn(2).setMinWidth(100);
        jocTable1.getColumnModel().getColumn(2).setPreferredWidth(100);
        jocTable1.getColumnModel().getColumn(2).setMaxWidth(110);
        jocTable1.getColumnModel().getColumn(3).setMinWidth(100);
        jocTable1.getColumnModel().getColumn(3).setPreferredWidth(100);
        jocTable1.getColumnModel().getColumn(3).setMaxWidth(110);
        jocTable1.getColumnModel().getColumn(4).setMinWidth(50);
        jocTable1.getColumnModel().getColumn(4).setPreferredWidth(50);
        jocTable1.getColumnModel().getColumn(4).setMaxWidth(60);
        jocTable2.setColumnSelectionAllowed(true);
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(
            org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, fleetList, jocTable2);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fleetid}"));
        columnBinding.setColumnName("Fleet ID");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fleetname}"));
        columnBinding.setColumnName("Fleet Name");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${votes}"));
        columnBinding.setColumnName("Votes");
        columnBinding.setColumnClass(Integer.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jocTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jocTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jocTable2);
        jocTable2.getColumnModel().getSelectionModel().setSelectionMode(
            javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jocTable2.getColumnModel().getColumn(0).setMinWidth(50);
        jocTable2.getColumnModel().getColumn(0).setPreferredWidth(50);
        jocTable2.getColumnModel().getColumn(0).setMaxWidth(60);
        jocTable2.getColumnModel().getColumn(1).setMinWidth(150);
        jocTable2.getColumnModel().getColumn(1).setPreferredWidth(150);
        jocTable2.getColumnModel().getColumn(1).setMaxWidth(160);
        jocTable2.getColumnModel().getColumn(2).setMinWidth(50);
        jocTable2.getColumnModel().getColumn(2).setPreferredWidth(50);
        jocTable2.getColumnModel().getColumn(2).setMaxWidth(60);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            "Truck Make", "Peterbilt", "Kenworth", "FreightLiner", "Inernational", "Mack", "Sterling", "Volvo", "Ford",
            "Dodge"
        }));
        jComboBox1.setSelectedItem("Truck Make");
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jTextField1.setText("Truck Owner / Fleet");
        jTextField1.setToolTipText("");
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            "Truck Class", "BobTail", "Combo", "Straight Truck", "Classic", "Wrecker"
        }));
        jComboBox2.setSelectedItem("Truck Class");
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });
        jButton1.setText("Add Entry");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton2.setText("Add Fleet");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jocLabel1.setAntialias(true);
        jocLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jocLabel1.setLabelFor(this);
        jocLabel1.setText("ECLC Annual Truck Show");
        jocLabel1.setTextPlacement(com.xzq.osc.OrchidUtils.TOP);
        jocLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));      // NOI18N
        jocLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jocLabel2.setText("Choose Winners");
        jocLabel2.setFont(new java.awt.Font("Segoe UI", 1, 12));    // NOI18N
        jocLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jocLabel3.setText("Create new Entry");
        jocLabel3.setFont(new java.awt.Font("Segoe UI", 1, 12));    // NOI18N
        jButton3.setText("BobTail");
        jButton3.setToolTipText("");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jButton4.setText("Combo");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jButton5.setText("Classic");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jButton6.setText("Straight Truck");
        jButton6.setToolTipText("");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jButton7.setText("Wrecker");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jMenu1.setText("File");
        jMenuItem1.setText("Exit");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);
        jMenuBar1.add(jMenu1);
        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                        jocLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(
                        layout.createSequentialGroup().addComponent(
                            jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 504, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(
                            18, 18, 18).addComponent(
                            jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)).addGroup(
                                layout.createSequentialGroup().addGroup(
                                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                                        jocLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE).addGroup(
                                        layout.createSequentialGroup().addGap(100, 100, 100).addComponent(
                                            jButton1).addGap(48, 48, 48).addComponent(jButton2)).addGroup(
                                                layout.createSequentialGroup().addComponent(
                                                    jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(
                                                        18, 18, 18).addComponent(
                                                            jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(
                                                                18, 18, 18).addComponent(
                                                                    jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))).addGroup(
                                                                        layout.createParallelGroup(
                                                                            javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                                                                                javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(
                                                                                    102, 102, 102).addComponent(
                                                                                        jocLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGroup(
                                                                                            javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addPreferredGap(
                                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(
                                                                                                    layout.createParallelGroup(
                                                                                                        javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                                                                                                            javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(
                                                                                                                jButton3).addGap(
                                                                                                                    44, 44, 44).addComponent(
                                                                                                                        jButton4).addGap(
                                                                                                                            39, 39, 39).addComponent(
                                                                                                                                jButton5)).addGroup(
                                                                                                                                    javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(
                                                                                                                                        jButton6).addGap(
                                                                                                                                            30, 30, 30).addComponent(
                                                                                                                                                jButton7).addGap(
                                                                                                                                                    51, 51, 51))))))).addContainerGap()));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap().addComponent(
                    jocLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE).addGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(
                            jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE).addComponent(
                            jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)).addGap(
                                18, 18, 18).addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                    jocLabel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                    javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                        jocLabel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
                                            javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
                                                layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                        jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                                                    jTextField1,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                                                                    jComboBox2,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                                                                                    jButton3).addComponent(
                                                                                                        jButton4).addComponent(
                                                                                                            jButton5)).addGap(
                                                                                                                18, 18,
                                                                                                                    18).addGroup(
                                                                                                                        layout.createParallelGroup(
                                                                                                                            javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                                                                                                jButton2).addComponent(
                                                                                                                                    jButton1).addComponent(
                                                                                                                                        jButton6).addComponent(
                                                                                                                                            jButton7)).addGap(
                                                                                                                                                37,
                                                                                                                                                37,
                                                                                                                                                37)));
        bindingGroup.bind();
        pack();
    }    // </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jMenuItem1ActionPerformed
        System.exit(0);
    }    // GEN-LAST:event_jMenuItem1ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jComboBox1ActionPerformed
        if (null != jComboBox1.getSelectedItem()) {
            _createEntryTruckMake = jComboBox1.getSelectedItem().toString();
        } else {
            _createEntryTruckMake = "EMPTY";
        }
    }    // GEN-LAST:event_jComboBox1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jComboBox2ActionPerformed
        if (null != jComboBox2.getSelectedItem()) {
            _createEntryTruckClass = jComboBox2.getSelectedItem().toString();
        } else {
            _createEntryTruckClass = "EMPTY";
        }
    }    // GEN-LAST:event_jComboBox2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jButton1ActionPerformed
        if (null != jTextField1.getText()) {
            _createEntryTruckOwner = jTextField1.getText();
        } else {
            _createEntryTruckOwner = "NONE";
        }

        try {
            createEntry();
        } catch (PreexistingEntityException ex) {
            LOG.log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }                                                                      // GEN-LAST:event_jButton1ActionPerformed

    private void jocTable1MouseClicked(java.awt.event.MouseEvent evt) {    // GEN-FIRST:event_jocTable1MouseClicked
        int tmpRow = jocTable1.getSelectedRow();
        int tmpCol = jocTable1.getSelectedColumn();

        updateEntryVotes(tmpCol, tmpRow);
    }                                                                      // GEN-LAST:event_jocTable1MouseClicked

    private void jocTable2MouseClicked(java.awt.event.MouseEvent evt) {    // GEN-FIRST:event_jocTable2MouseClicked
        int tmpRow = jocTable2.getSelectedRow();
        int tmpCol = jocTable2.getSelectedColumn();

        updateFleetVotes(tmpRow, tmpCol);
    }                                                                         // GEN-LAST:event_jocTable2MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jButton2ActionPerformed
        if (jTextField1 != null) {
            _fleetName = jTextField1.getText();
        } else {
            _fleetName = "EMPTY";
        }

        createFleetEntry();
    }                                                                         // GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jButton3ActionPerformed
        Winners win = new Winners();

        win.showWinners("BobTail");
    }                                                                         // GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jButton4ActionPerformed
        Winners win = new Winners();

        win.showWinners("Combo");
    }                                                                         // GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jButton5ActionPerformed
        Winners win = new Winners();

        win.showWinners("Classic");
    }                                                                         // GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jButton6ActionPerformed
        Winners win = new Winners();

        win.showWinners("Straight Truck");
    }                                                                         // GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_jButton7ActionPerformed
        Winners win = new Winners();

        win.showWinners("Wrecker");
    }    // GEN-LAST:event_jButton7ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        /* Set the Nimbus look and feel */

        // <editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

        /*
         *  If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());

                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            LOG.log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            LOG.log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            LOG.log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            LOG.log(java.util.logging.Level.SEVERE, null, ex);
        }

        // </editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MainForm mFrm = null;

                try {
                    mFrm = new MainForm();
                } catch (PreexistingEntityException ex) {
                    LOG.log(Level.SEVERE, "Entry creation failed", ex);
                } catch (Exception ex) {
                    LOG.log(Level.SEVERE, "Entry Creation failed", ex);
                }

                mFrm.setLocationRelativeTo(null);
                mFrm.pack();
                mFrm.setVisible(true);
            }
        });
    }

    /**
     *
     */
    public void getEntryTableValues() {
        _ID = (int) jocTable1.getModel().getValueAt(tcl.getRow(), 0);
        _votes = (int) jocTable1.getModel().getValueAt(tcl.getRow(), 4);
        _truckOwner = (String) jocTable1.getModel().getValueAt(tcl.getRow(), 1);
        _truckMake = (String) jocTable1.getModel().getValueAt(tcl.getRow(), 2);
        _truckClass = (String) jocTable1.getModel().getValueAt(tcl.getRow(), 3);
    }

    /**
     *
     */
    public void createEntryEdit() {
        Entry en = new Entry();

        en.setTruckid(_ID);
        en.setTruckowner(_truckOwner);
        en.setTruckmake(_truckMake);
        en.setTruckclass(_truckClass);
        en.setVotes(_votes);

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ShowTestPU");
        EntryJpaController ejc = new EntryJpaController(emf);

        try {
            ejc.edit(en);
        } catch (PreexistingEntityException ex) {
            LOG.log(Level.SEVERE, "Edit FAILED - Entry does not exist", ex);
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, "Edit FAILED - See stack trace for details", ex);
        }
    }

    private void createFleetEdit() {
        Fleet ft = new Fleet();

        ft.setFleetid(_fleetID);
        ft.setFleetname(_fleetName);
        ft.setVotes(_fleetVotes);

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ShowTestPU");
        FleetJpaController ejc = new FleetJpaController(emf);

        try {
            ejc.edit(ft);
        } catch (PreexistingEntityException ex) {
            LOG.log(Level.SEVERE, "Edit FAILED - Entry does not exist", ex);
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, "Edit FAILED - See stack trace for details", ex);
        }
    }

    private void createFleetEntry() {
        _fleetID = jocTable2.getRowCount() + 1;
        _fleetVotes = 0;

        Fleet ft = new Fleet();

        ft.setFleetid(_fleetID);
        ft.setFleetname(_fleetName);
        ft.setVotes(_fleetVotes);

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ShowTestPU");
        FleetJpaController ejc = new FleetJpaController(emf);

        try {
            ejc.create(ft);
        } catch (PreexistingEntityException ex) {
            LOG.log(Level.SEVERE, "Edit FAILED - Entry does not exist", ex);
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, "Edit FAILED - See stack trace for details", ex);
        }
    }

    private void getFleetTableValues() {
        _fleetVotes = (int) jocTable2.getModel().getValueAt(tclFleet.getRow(), 2);
        _fleetName = (String) jocTable2.getModel().getValueAt(tclFleet.getRow(), 1);
        _fleetID = (int) jocTable2.getModel().getValueAt(tclFleet.getRow(), 0);
    }

    private void createEntry() throws PreexistingEntityException, Exception {
        _ID = jocTable1.getRowCount() + 1;

        Entry en = new Entry();

        en.setTruckowner(_createEntryTruckOwner);
        en.setTruckmake(_createEntryTruckMake);
        en.setTruckclass(_createEntryTruckClass);
        en.setVotes(0);
        en.setTruckid(_ID);

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ShowTestPU");
        EntryJpaController ejc = new EntryJpaController(emf);

        ejc.create(en);
    }

    private void updateEntryVotes(int tmpCol, int tmpRow) {
        if (tmpCol == 4) {
            int tmp = (int) jocTable1.getModel().getValueAt(tmpRow, 4);

            tmp += 1;
            jocTable1.getModel().setValueAt(tmp, tmpRow, 4);
            _votes = tmp;
            _ID = (int) jocTable1.getModel().getValueAt(tmpRow, 0);
            _truckOwner = (String) jocTable1.getModel().getValueAt(tmpRow, 1);
            _truckMake = (String) jocTable1.getModel().getValueAt(tmpRow, 2);
            _truckClass = (String) jocTable1.getModel().getValueAt(tmpRow, 3);
            createEntryEdit();
        }
    }

    private void updateFleetVotes(int tmpRow, int tmpCol) {
        if (tmpCol == 2) {
            int tmp = (int) jocTable2.getModel().getValueAt(tmpRow, 2);

            tmp += 1;
            jocTable2.getModel().setValueAt(tmp, tmpRow, 2);
            _fleetVotes = (int) jocTable2.getModel().getValueAt(tmpRow, 2);
            _fleetName = (String) jocTable2.getModel().getValueAt(tmpRow, 1);
            _fleetID = (int) jocTable2.getModel().getValueAt(tmpRow, 0);
            createFleetEdit();
        }
    }

    /**
     * @return the actionFleet
     */
    public Action getActionFleet() {
        return actionFleet;
    }

    /**
     * @param actionFleet the actionFleet to set
     */
    public void setActionFleet(Action actionFleet) {
        this.actionFleet = actionFleet;
    }
}


//~ Formatted in DD Std on 13/08/20
